<?php
/**
 * @see Alinks_Form_Element_Abstract
 */
require_once('Alinks/Form/Element/Abstract.php');

/**
 * Represents a group of radio button elements
 *
 * Copyright (C) 2008  Sean Hickey
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * @author Sean Hickey <sean@headzoo.com>
 * @version $Id: RadioGroup.php 378 2008-07-03 05:19:31Z sean $
 * @package Alinks_Form
 */
class Alinks_Form_Element_RadioGroup extends Alinks_Form_Element_Abstract
{
	/**
	 * The radio buttons in the group
	 *
	 * @var Alinks_Form_Element_Radio[]
	 */
	protected $_radios = array();
	
	/**
	 * The string that goes between each radio button
	 *
	 * @var string
	 */
	protected $_step = '';
	
	/**
	 * Constructor
	 *
	 * @param string $name The name of the radio buttons
	 * @param string $label The label describing the group
	 * @param string $step String that goes inbetween each radio button
	 */
	public function __construct($name = null, $label = null, $step = ' ')
	{
		$this->setName($name);
		$this->setLabel($label);
		$this->setStep($step);
	}
	
	/**
	 * Adds a radio button to the group
	 *
	 * @param Alinks_Form_Element_Radio $radio The radio button
	 * @return Alinks_Form_Element_RadioGroup
	 */
	public function addRadio(Alinks_Form_Element_Radio $radio)
	{
		$radio->setName($this->getName());
		$this->_radios[] = $radio;
		return $this;
	}
	
	/**
	 * Returns the radio buttons in the group
	 *
	 * @return Alinks_Form_Element_Radio[]
	 */
	public function getRadios()
	{
		return $this->_radios;
	}
	
	/**
	 * Sets the string that goes between each radio button
	 *
	 * @param string $step The string
	 * @return Alinks_Form_Element_RadioGroup
	 */
	public function setStep($step)
	{
		$this->_step = $step;
		return $this;
	}
	
	/**
	 * Returns the string that goes between each radio button
	 *
	 * @return string
	 */
	public function getStep()
	{
		return $this->_step;
	}
	
	/**
	 * Returns the HTML for the form element
	 *
	 * @return string
	 */
	public function getHtml()
	{
		$html = '<div class="alinks_radio_group">';
		$html .= '<div class="alinks_radio_group_label">' . $this->getRawLabel() . '</div>';
		foreach($this->_radios as $radio) {
			$radio->setId($radio->getId() . '_' . $radio->getValue());
			$radio_html = $radio->getHtml();
			if ($radio->getValue() != $this->getValue()) {
				$radio_html = str_replace('checked="checked"', '', $radio_html);
			}
			$html .= $radio_html;
			$html .= $this->_step;
		}
		$html .= '</div>';
		
		return $html;
	}
}